<?php
/** * Alerts_model Class * 
* @package TIAP 
* @subpackage Model 
* @category alerts 
* @author ibnu triyono
* @link http://twitter.com/ibnutri 
*/
class Alerts_model extends MY_Model{
  var $settings_table;
  
  function Alerts_model(){
    parent::MY_Model();
    $this->alerts_table='alerts';
  }
  
  
  /**
  *    get_alerts method
  *
  *    @description get alerts list with condition network id or user id or both
  *    @param     string $network_id
  *    @param     string $order_by field mana yang mau di urutkan
  *    @param     string $order bagaimana pengurutannya (ASC/DESC)
  *    @note perhatikan bahwa hanya jika order_by dan order terisi akan diurutkan
  *    @result array
  */
  
  function get_alerts($network_id='',$order_by ='id_network',$order='DESC',$offset='',$limit='10'){    
    if($network_id!= '' ){
    $this->db->where('id_network', $network_id);    
    }        
    if($order_by != '' && $order != ''){
      $this->db->order_by($order_by,$order);
    }
    $query = $this->db->get($this->alerts_table,$limit,$offset );
    if($query->num_rows() > 0){
      return $query->result_array();
    }else{
      return FALSE;
    }	    
  }

  /**
  *    get_count_alerts method
  *
  *    @description get alerts list count with condition network id or user id or both
  *    @param     string $network_id
  *    @result integer
  */
  
  function get_count_alerts($network_id){    
    $this->db->where('id_network', $network_id);
    $alert_count = $this->db->count_all_results($this->alerts_table);
      return $alert_count;
  }

  /**
  *    insert_alert method
  *
  *    @description inserting alerts but not with the images
  *    @param     string $network_id
  *    @result alerts_id INT
  */
  function insert_alert($id_user,$id_network, $short_alert='', $post_to_email='',$post_to_twitter='',$post_to_sms='',$alert_detail=''){
    $post_to_sms = ($post_to_sms != '')?'yes':'no';
    $post_to_email = ($post_to_email != '')?'yes':'no';
    $post_to_twitter = ($post_to_twitter != '')?'yes':'no';
    $array_insert = array(
      'id_user'=>$id_user,
      'id_network'=>$id_network,
      'short_alert'=>$short_alert,
      'post_to_email'=>$post_to_email,
      'post_to_twitter'=>$post_to_twitter,
      'post_to_sms'=>$post_to_sms,
      'alert_detail'=>$alert_detail,
      'created_date'=>date('Y-m-d H:i:s'),
    );
    $return = $this->db->insert($this->alerts_table,$array_insert);
    if($return){
	    $return = $this->db->insert_id();
	    log_message('debug','successfully insert alerts to database');
    }else{
	    log_message('error','failed to insert alerts to database');
    }
    return $return;
  }
  
    function update_alert($id_alerts,$id_user,$id_network, $post_to_email='',$post_to_twitter='',$post_to_sms='',$alert_detail=''){
        $post_to_sms = ($post_to_sms != '')?'yes':'no';
        $post_to_email = ($post_to_email != '')?'yes':'no';
        $post_to_twitter = ($post_to_twitter != '')?'yes':'no';
        $array_update = array(
            'id_user'=>$id_user,
            'id_network'=>$id_network,            
            'post_to_email'=>$post_to_email,
            'post_to_twitter'=>$post_to_twitter,
            'post_to_sms'=>$post_to_sms,
            'alert_detail'=>$alert_detail,
            //'created_date'=>date('Y-m-d H:i:s'),
        );
        $this->db->where('id', $id_alerts);
        //$return = $this->db->insert($this->alerts_table,$array_insert);
        $return = $this->db->update($this->alerts_table, $array_update); 
        if($return){
            $return = $this->db->insert_id();
            log_message('debug','successfully insert alerts to database');
        }else{
            log_message('error','failed to insert alerts to database');
        }
        return $return;
    } 
    /**
    * method insert_images
    * upload image and create thumbnail in alerts
    * path real : ./uploads/profile/real/
    * path thumbnail : ./uploads/profile/thumbnail/
    * 
    * @return string boolean
    * 
    */
    function insert_images($upload, $image_text, $number, $alert_id){
      /*$config['upload_path'] = './uploads/alerts/real/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size']	= '1000';
      $config['max_width']  = '1024';
      $config['max_height']  = '768';
      $config['encrypt_name']  = TRUE;
		
      $this->load->library('upload', $config);
      for($i = 1; $i<=3;$i++){
	  if (  $this->upload->do_upload('image_'.$i)){
	  $upload_result = $this->upload->data();*/
	  $array_images = array(
	    'image_'.$number            => $upload,
	    'image_'.$number.'_caption' => $image_text,
	    );
	  $this->db->where('id',$alert_id);
	  $this->db->update($this->alerts_table,$array_images);
	//}else{
	  //echo $this->upload->display_errors();
	//}
      //} 
    }
    /*
     * method get_alert_detail
     * @description get detail of crime report from database
     * @parent $crime_report_id = id of the wanted crime report
     *
     */
    function get_alert_detail($alert_id='',$network_id=''){
      $this->db->where('id', $alert_id);
      $this->db->where('id_network', $network_id);
      $query = $this->db->get($this->alerts_table,1);
      if($query->num_rows() > 0){
	return $query->row_array();
      }else{
	return FALSE;
      }

    }
    /**
    * method insert_comment_alerts
    * insert comment for alert
    * 
    * @param mixed $alerts_id
    * @param mixed $user_tiab_id
    * @param mixed $comment
    * 
    * @return string query
    */
    function insert_comment_alerts($alerts_id, $user_tiab_id, $comment){
        $array_insert = array(
            'alerts_id' => $alerts_id,
            'user_tiab_id' => $user_tiab_id,
            'comment' => $comment,
            'created_date' => date('Y-m-d H:i:s'),
        );
        $this->db->insert('alerts_comment',$array_insert);
        return $this->db->last_query();
    }
    
    /**
    * method get_comment_alert
    * show all comment based on alerts id
    *  
    * @param mixed $alerts_id 
    * @return array results
    */    
    function get_comment_alert($alerts_id){
        /*$this->db->where('alerts_id', $alerts_id);
        $query = $this->db->get('alerts_comment');
        //return $this->db->last_query();
        if($query->num_rows() > 0){
            return $query->result_array();
        }else{
            return FALSE;
        }*/
        
        $this->db->select('user_tiab.id, user_tiab.firstname, user_tiab.surname, user_tiab.avatar, alerts_comment.*');
        $this->db->from('alerts_comment');
        $this->db->join('user_tiab', 'alerts_comment.user_tiab_id = user_tiab.id', 'left');
        $this->db->where('alerts_comment.alerts_id',$alerts_id);
        $this->db->order_by('alerts_comment.created_date','desc');        
        $query = $this->db->get();        
        //return $this->db->last_query();
        if($query->num_rows() > 0){
            return $query->result_array();
        }else{
            return FALSE;
        }    
    }
    /**
    * function get_count_alerts_comment
    * count comment from specified alert
    * 
    * @param mixed $alerts_id
    * 
    * @return integer count all results
    */
    function get_count_alerts_comment($alerts_id, $network_id=""){    
        $sql = "select * from tbl_comments where comment_for_url like '%network/read_alert/".$network_id."/".$alerts_id."%'";
        $this->db->flush_cache();
        $query = $this->db->query($sql);
        return $query->num_rows();
        //$this->db->where('alerts_id', $alerts_id);
        //return $this->db->count_all_results('alerts_comment');         
    }
    /**
    * function insert_like_alerts
    * insert data to alerts like table
    * 
    * @param mixed $alerts_id
    * @param mixed $user_tiab_id
    * 
    * @return string query
    */
    function insert_like_alerts($alerts_id, $user_tiab_id){
        $array_insert = array(
            'alerts_id' => $alerts_id,
            'user_tiab_id' => $user_tiab_id,            
            'created_date' => date('Y-m-d H:i:s'),
        );
        $this->db->insert('alerts_like',$array_insert);
        return $this->db->last_query();
    }
    /**
    * function get_like_alert
    * show all data from table alerts like and give it whos like the aletrs
    * 
    * @param mixed $alerts_id
    * @return array results
    */
    function get_like_alert($alerts_id){
        $this->db->select('user_tiab.id, user_tiab.firstname, user_tiab.surname, user_tiab.avatar, alerts_like.*');
        $this->db->from('alerts_like');
        $this->db->join('user_tiab', 'alerts_like.user_tiab_id = user_tiab.id', 'left');
        $this->db->where('alerts_like.alerts_id',$alerts_id);
        $this->db->order_by('alerts_like.created_date','desc');        
        $query = $this->db->get();        
        //return $this->db->last_query();
        if($query->num_rows() > 0){
            return $query->result_array();
        }else{
            return FALSE;
        }    
    }
    
    /**
    * function get_count_alerts_like
    * count all user that like specified alerts
    * 
    * @param mixed $alerts_id
    * @return integer count all results
    */
    function get_count_alerts_like($alerts_id){    
        $this->db->where('alerts_id', $alerts_id);
        return $this->db->count_all_results('alerts_like');         
    }
    
    /**
    * function delete_images
    * empty field image, and delete the image
    * 
    * @param mixed $alert_id
    * @param mixed $image_1
    * @param mixed $image_2
    * @param mixed $image_3
    */
    function delete_images($alert_id, $image_1='', $image_2='', $image_3=''){
        if($image_1){
            unlink('./uploads/alerts/real/'.$image_1);
            $array_images1 = array(
                'image_1'=>'',
                'image_1_caption'=>'',
            );
            $this->db->where('id',$alert_id);
            $this->db->update($this->alerts_table,$array_images1);
        }
        if($image_2){
            unlink('./uploads/alerts/real/'.$image_2);
            $array_images2 = array(
                'image_2'=>'',
                'image_2_caption'=>'',
            );
            $this->db->where('id',$alert_id);
            $this->db->update($this->alerts_table,$array_images2);
        }
        if($image_3){
            unlink('./uploads/alerts/real/'.$image_3);
            $array_images3 = array(
                'image_3'=>'',
                'image_3_caption'=>'',
            );
            $this->db->where('id',$alert_id);
            $this->db->update($this->alerts_table,$array_images3);
        }                
    }
    /**
    * function update_embed
    * update alert with embed code
    * 
    * @param mixed $alert_id
    * @param mixed $embed_url
    */
    function update_embed($alert_id, $embed_url){
        $array_embed = array(
            'embed_url' => $embed_url
        );
        $this->db->where('id',$alert_id);
        $this->db->update($this->alerts_table, $array_embed);    
    }
    
    /**
    * function delete_alert
    * delete alert in table alert and alerts_comment, alerts_like 
    * where alert_id $alert_id 
    * 
    * @param mixed $alert_id
    */
    function delete_alert($id_alerts = ''){
        if($id_alerts != ''){
            $this->db->where('id',$id_alerts);
            $this->db->delete($this->alerts_table);
            $this->db->where('alerts_id',$id_alerts);
            $this->db->delete('alerts_comment');
            $this->db->where('alerts_id',$id_alerts);
            $this->db->delete('alerts_like');
            return TRUE;
        }else{
            return FALSE;
        }    
    }

    /**
    * function send_alert_email
    * send email alert to member, managers, creator
    * 
    * @param mixed $email
    * @param mixed $short_alert
    * @param mixed $alert_detail
    * @param mixed $username
    * @param mixed $created_date
    * @param mixed $alert_url
    */
    function send_alert_email($email='', $short_alert, $alert_detail, $network_name, $created_date, $alert_url, $network_url){
        $config = array(
            'mailtype'=>'html'
        );
        $data = array(                              
            'short_alert' => $short_alert,
            'alert_detail' => $alert_detail,
            'network_name' => $network_name,                            
            'created_date'=> $created_date,
            'alert_url' => $alert_url,
            'network_url' => $network_url,         
        );
        $email_content = $this->load->view(TEMPLATE_NAME.'/front/mail/crime_alert',$data, true);
        $this->load->library('email');
        $this->email->initialize($config);
        $this->email->from('noreply@turnitaround.co.za', 'Turn It Around');
        $this->email->to($email);        
        $this->email->subject('Crime alert reported');
        $this->email->message($email_content);
        
        $this->email->send();
    }
}

/* End of file settings_model.php */
/* Location: ./system/application/models/settings_model.php */
